package com.zhongke.common.viewadapter

import android.view.View
import androidx.core.view.isVisible
import androidx.databinding.BindingAdapter
import com.zhongke.common.viewadapter.command.BindingCommand
import com.zhongke.common.utils.singleClick

/**
 *@Date: 2022/2/11
 *@Time: 15:14
 *@Author:cxc
 *@Description:View的绑定类
 */
object ViewAdapter {

    /**
     *  view的显示隐藏
     * @param view View
     * @param visibility Boolean 是否可见
     */
    @BindingAdapter(value = ["isVisible"], requireAll = false)
    @JvmStatic
    fun isVisible(view: View, visibility: Boolean = true) {
        view.isVisible = visibility
    }

    /**
     *  View的点击事件
     * @param view View
     * @param method Function1<View, Unit>  点击回调方法
     */
    @BindingAdapter(value = ["viewClick"], requireAll = false)
    @JvmStatic
    fun onClickFuntion(view: View,method:(View)->Unit ) {
        view.singleClick {
               method.invoke(view)
        }
    }

    /**
     * View的点击事件
     * @param view View
     * @param delay Long 重复点击的延迟
     * @param bindingCommand BindingCommand<*>?
     */
    @BindingAdapter(value = ["bindClick", "clickDelay"], requireAll = false)
    @JvmStatic
    fun onClick(view: View, bindingCommand: BindingCommand<View>?,delay: Long ) {
        view.singleClick(delay=if (delay==0L) 2000 else delay) {
            bindingCommand?.execute(it)
        }
    }

    /**
     * View的longClick事件
     * @param view View
     * @param bindingCommand BindingCommand<*>?
     */
    @BindingAdapter(value = ["bindLongClick"], requireAll = false)
    @JvmStatic
    fun onLongClick(view: View, bindingCommand: BindingCommand<View>?) {
        view.setOnLongClickListener {
            bindingCommand?.execute(it)
            true
        }
    }


}